name: Scripts

on:
 push:
  paths:
  - '.github/workflows/scripts.yml'
  - '.github/workflows/scripts/compile-ssl.sh'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts/**/*.[Ss][Ss][Ll]'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts/**/*.[Hh]'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts.misc/**/*.[Ss][Ss][Ll]'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts.misc/**/*.[Hh]'
  - 'Fallout2/Fallout1in2/mods/**/*.[Ss][Ss][Ll]'
  - 'Fallout2/Fallout1in2/mods/**/*.[Hh]'
  - 'Tools/SFallScriptEditor/resources/compile.exe'
  - 'Tools/ReDefine/ReDefine.exe'
  - 'Tools/ReDefine/ReDefine.cfg'
 pull_request:
  paths:
  - '.github/workflows/scripts.yml'
  - '.github/workflows/scripts/compile-ssl.sh'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts/**/*.[Ss][Ss][Ll]'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts/**/*.[Hh]'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts.misc/**/*.[Ss][Ss][Ll]'
  - 'Fallout2/Fallout1in2/Mapper/source/scripts.misc/**/*.[Hh]'
  - 'Fallout2/Fallout1in2/mods/**/*.[Ss][Ss][Ll]'
  - 'Fallout2/Fallout1in2/mods/**/*.[Hh]'
  - 'Tools/SFallScriptEditor/resources/compile.exe'
  - 'Tools/ReDefine/ReDefine.exe'
  - 'Tools/ReDefine/ReDefine.cfg'

defaults:
 run:
  shell: bash

env:
 GHA_SCRIPTS: ".github/workflows/scripts"

jobs:

 Compile:
  if: github.actor != 'antalaskaya'
  runs-on: windows-latest
  steps:

  - name: Clone
    uses: actions/checkout@v4

  - name: Compile (base mod)
    run:  $GHA_SCRIPTS/compile-ssl.sh

  - name: Compile (mini mods)
    run:  $GHA_SCRIPTS/compile-ssl.sh --scripts-dir=Fallout2/Fallout1in2/mods/

  - name: Compile (misc)
    run:  $GHA_SCRIPTS/compile-ssl.sh --scripts-dir=Fallout2/Fallout1in2/Mapper/source/scripts.misc/
    continue-on-error: true

 ###

 ReDefine:
  needs:   Compile
  runs-on: windows-latest
  steps:

  - name: Clone
    uses: actions/checkout@v4

  - name: ReDefine
    run:  |
          :
          scripts=$(grep "^ScriptsDir" Tools/ReDefine/ReDefine.cfg | awk '{print $3}' | sed -re 's!^[\.\/]+!!')
          headers=$(grep "^HeadersDir" Tools/ReDefine/ReDefine.cfg | awk '{print $3}' | sed -re 's!^[\.\/]+!!')

          Tools/ReDefine/ReDefine.exe --config Tools/ReDefine/ReDefine.cfg --scripts $scripts/ --headers $headers/ --debug-changes 1 >/dev/null

          echo ::group::Run results
          if [ -f "ReDefine.log" ] && grep --quiet --max-count=1 "^@@" ReDefine.log; then
             echo REDEFINE_CHANGES=true >> $GITHUB_ENV
          fi
          if [ -f "ReDefine.log" ] && grep --quiet --max-count=1 "^%%" ReDefine.log; then
             echo REDEFINE_USERLOG=true >> $GITHUB_ENV
          fi
          if [ -f "ReDefine.WARNING.log" ]; then
             echo REDEFINE_WARNING=true >> $GITHUB_ENV
          fi
          if [ -f "ReDefine.DEBUG.log" ]; then
             echo REDEFINE_DEBUG=true >> $GITHUB_ENV
             sed -ri 's!^DEBUG script code !\nDEBUG script code !g' ReDefine.DEBUG.log
             sed -ri '1{/^$/d}' ReDefine.DEBUG.log
             sed -ri 's!^DEBUG !!g' ReDefine.DEBUG.log
          fi
          echo ::endgroup::

  - name: Changes
    if:   env.REDEFINE_CHANGES == 'true'
    run:  grep --after-context=2 "^@@" ReDefine.log

  - name: UserLog
    if:   env.REDEFINE_USERLOG == 'true'
    run:  grep --after-context=1 "^%%" ReDefine.log

  - name: Warnings
    if:   env.REDEFINE_WARNING == 'true'
    run:  cat ReDefine.WARNING.log

  - name: Debug
    if:   env.REDEFINE_DEBUG == 'true'
    run:  cat ReDefine.DEBUG.log

  - name: Cleanup
    run:  rm ReDefine*.log && git status

  - name: Compile
    if:   env.REDEFINE_CHANGES == 'true'
    run:  $GHA_SCRIPTS/compile-ssl.sh
